package utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

public class MyBatisUtils {

    private static SqlSessionFactory sqlSessionFactory = null;
    //    初始化SqlSessionFactory对象
    static {
        try{
//        加载mybatis配置文件
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//        构建sqlsessionfactory工厂
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //    获取sqlsession对象的静态方法
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }

}
